package com.gba.mark.exam.controller;


import com.gba.mark.exam.entity.ExamQuestions;
import com.gba.mark.exam.service.ExamQuestionsService;
import com.gba.mark.group.service.IGroupService;
import com.gba.mark.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/exam/questions")
public class ExamQuestionsController {

    @Autowired
    private ExamQuestionsService examQuestionsService;

    @Resource
    private IGroupService groupService;
    /**
     * 新建并保存一份题目
     */
    @PostMapping("/saveQuestion")
    public ResponseUtil<Integer> saveExamQuestions(@RequestBody ExamQuestions examQuestions) {
        return new ResponseUtil<>(HttpStatus.OK, "",examQuestionsService.saveQuestion(examQuestions));
    }


//    /*
//   从前端接收List保存并更新一份题目
//     */
//
//    @PostMapping("/receiveData")
//    public ResponseUtil<List<ExamQuestions>> receiveData(
//            @RequestParam List<ExamQuestions> list) {
//
//        return new ResponseUtil<>(HttpStatus.OK,
//                examQuestionsService.receiveData(list));
//}

    /*
    删除一个题目
     */

    @DeleteMapping("/deleteExamQuestions")
    public ResponseUtil<Boolean> deleteExamQuestions(Authentication authentication,
                                                     @RequestParam String groupName) {

        int group_id = groupService.getGroupIdByAuthAndName(authentication,groupName);
        return new  ResponseUtil<>(HttpStatus.OK,"",examQuestionsService.removeExamQuestions(group_id));
    }


    /*
    查询一个题目
     */

    @PostMapping("/findQuestion")
    public ResponseUtil<List<ExamQuestions>> findQuestion(Authentication authentication,
                                                          @RequestParam String groupName) {

        int group_id = groupService.getGroupIdByAuthAndName(authentication,groupName);
        return new ResponseUtil<>(HttpStatus.OK,"",
                examQuestionsService.findQuestion(group_id));


    }
}
